package cn.edu.lzzy.services.Impl;

import cn.edu.lzzy.dao.AllocationRepository;
import cn.edu.lzzy.entities.Allocation;
import cn.edu.lzzy.entities.User;
import cn.edu.lzzy.services.AllocationService;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@Service
public class AllocationServiceImpl implements AllocationService {
    private final AllocationRepository allocationRepository;

    @Autowired
    public AllocationServiceImpl(AllocationRepository allocationRepository) {
        this.allocationRepository = allocationRepository;
    }

    @Override
    public Allocation findAllocation(String url) {
        return allocationRepository.findAllocationByUrlEquals(url);
    }

    @Override
    public List<Allocation> finAll() {
        return allocationRepository.findAll();
    }

    @Override
    public void sysUrls(List<String> urls) {
            List<Allocation> saved=allocationRepository.findAll();
            List<Allocation> toBeDeleted=new ArrayList<>();
            saved.forEach(allocation -> {
                if(urls.stream().noneMatch(url->url.equals(allocation.getUrl()))){
                    toBeDeleted.add(allocation);
                }

            });
            List<Allocation> toBeAdded=new ArrayList<>();
            urls.forEach(url->{
                if(saved.stream().noneMatch(allocation -> allocation.getUrl().equals(url))){
                    Allocation added=new Allocation();
                    added.setUrl(url);
                    toBeAdded.add(added);

                }
            });
            for(Allocation allocation:toBeDeleted){
                allocationRepository.delete(allocation);
            }
            for(Allocation allocation:toBeAdded){
                allocationRepository.save(allocation);
            }
    }

    @Override
    public Allocation save(String url, int value) {
        Allocation allocation=allocationRepository.findAllocationByUrlEquals(url);
        if(allocation==null){
            allocation=new Allocation();
        }
        if(value>=0){
            allocation.setRoles(value);
        }
        allocation.setUrl(url);
        return allocationRepository.save(allocation);
    }

    @Override
    public List<Pair<Integer, String>> getUserTypes() {
        try {
            List<Pair<Integer,String>> pairs=new ArrayList<>();
            Field[] fields= User.class.getFields();
            for(Field field:fields){
                String name=field.getName();
                if(name.startsWith("TYPE_")){
                    int value=field.getInt(null);
                    if(value<0){
                        continue;
                    }
                    pairs.add(new Pair<>(value,name));
                }
            }
            return pairs;
        }catch (IllegalAccessError | IllegalAccessException ex){
            return null;
        }

    }
}
